home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / arc.zoo / arcrun.c < prev    next >
C/C++ Source or Header  |  1989-01-29  |  4KB  |  157 lines

  1. /*
  2.  * $Header: arcrun.c,v 1.4 88/07/31 18:52:50 hyc Exp $
  3.  */
  4.  
  5. /*
  6.  * ARC - Archive utility - ARCRUN
  7.  * 
  8.  * Version 1.20, created on 03/24/86 at 19:34:31
  9.  * 
  10.  * (C) COPYRIGHT 1985,85 by System Enhancement Associates; ALL RIGHTS RESERVED
  11.  * 
  12.  * By:  Thom Henderson
  13.  * 
  14.  * Description: This file contains the routines used to "run" a file which is
  15.  * stored in an archive.  At present, all we really do is (a) extract a
  16.  * temporary file, (b) give its name as a system command, and then (c) delete
  17.  * the file.
  18.  * 
  19.  * Language: Computer Innovations Optimizing C86
  20.  */
  21. #include <stdio.h>
  22. #include "arc.h"
  23.  
  24. void    rempath(), openarc(), closearc(), abort();
  25. int    readhdr(), match(), unpack();
  26. static    void    runfile();
  27. #ifndef __STDC__
  28. char    *strcat();
  29. #endif
  30.  
  31. void
  32. runarc(num, arg)        /* run file from archive */
  33.     int             num;    /* number of arguments */
  34.     char           *arg[];    /* pointers to arguments */
  35. {
  36.     struct heads    hdr;    /* file header */
  37.     char           *makefnam();    /* filename fixer */
  38.     char            buf[STRLEN];    /* filename buffer */
  39. #ifndef __STDC__
  40.     FILE           *fopen();/* file opener */
  41. #endif
  42.     char           *dummy[2];
  43.  
  44.     dummy[0]="dummy";
  45.     dummy[1]=NULL;
  46.     rempath(num, arg);    /* strip off paths */
  47.  
  48.     openarc(0);        /* open archive for reading */
  49.  
  50.     if (num) {        /* if files were named */
  51.         while (readhdr(&hdr, arc)) {    /* while more files to check */
  52.             if (match(hdr.name, makefnam(arg[0], ".*", buf)))
  53.                 runfile(&hdr, num, arg);
  54.             else
  55.                 fseek(arc, hdr.size, 1);
  56.         }
  57.     } else
  58.         while (readhdr(&hdr, arc))    /* else run all files */
  59.             runfile(&hdr, 1, dummy);
  60.  
  61.     closearc(0);        /* close archive after changes */
  62. }
  63.  
  64. static  void
  65. runfile(hdr, num, arg)        /* run a file */
  66.     struct heads   *hdr;    /* pointer to header data */
  67.     int             num;    /* number of arguments */
  68.     char           *arg[];    /* pointers to arguments */
  69. {
  70.     FILE           *tmp;    /* temporary file */
  71. #ifndef __STDC__
  72.     FILE           *fopen();
  73. #endif
  74.     char           *dir, *gcdir();    /* directory stuff */
  75.     char            buf[STRLEN], *makefnam();    /* temp file name, fixer */
  76. #if    DOS
  77.     char        nbuf[64], *i, *rindex();
  78. #endif
  79. #if    !GEMDOS
  80.     int             n;    /* index */
  81.     char            sys[STRLEN];    /* invocation command buffer */
  82. #endif
  83.  
  84.     /* makefnam("$ARCTEMP",hdr->name,buf); */
  85. #if    UNIX
  86.     sprintf(buf, "%s.RUN", arctemp);
  87.     strcpy(sys, buf);
  88. #else
  89.     strcpy(nbuf, arctemp);
  90.     makefnam(nbuf,hdr->name,buf);
  91.     i = rindex(buf,'.');
  92. #endif
  93. #if    MSDOS
  94.     if (!strcmp(i, ".BAS")) {
  95.         strcpy(sys, "BASICA ");
  96.         strcat(sys, buf);
  97.     }
  98.     else if (!strcmp(i, ".BAT")
  99.          || !strcmp(i, ".COM")
  100.          || !strcmp(i, ".EXE"))
  101.         strcpy(sys, buf);
  102.  
  103.     else {
  104.         if (warn) {
  105.             printf("File %s is not a .BAS, .BAT, .COM, or .EXE\n",
  106.                    hdr->name);
  107.             nerrs++;
  108.         }
  109.         fseek(arc, hdr->size, 1);    /* skip this file */
  110.         return;
  111.     }
  112. #endif
  113. #if    GEMDOS
  114.       if (strcmp(i, ".PRG")
  115.               && strcmp(i, ".TTP")
  116.               && strcmp(i, ".TOS"))
  117.       {
  118.               if (warn) {
  119.                       printf("File %s is not a .PRG, .TOS, or .TTP\n",
  120.                               hdr->name);
  121.                       nerrs++;
  122.               }
  123.               fseek(arc, hdr->size, 1);       /* skip this file */
  124.               return;
  125.       }
  126. #endif
  127.  
  128.     if (warn)
  129.         if (tmp = fopen(buf, "r"))
  130.             abort("Temporary file %s already exists", buf);
  131.     if (!(tmp = fopen(buf, OPEN_W)))
  132.         abort("Unable to create temporary file %s", buf);
  133.  
  134.     if (note)
  135.         printf("Invoking file: %s\n", hdr->name);
  136.  
  137.     dir = gcdir("");    /* see where we are */
  138.     unpack(arc, tmp, hdr);    /* unpack the entry */
  139.     fclose(tmp);        /* release the file */
  140.     chmod(buf, 0700);    /* make it executable */
  141. #if    GEMDOS
  142.     execve(buf, arg, (char **)NULL);
  143. #else
  144.     for (n = 1; n < num; n++) {    /* add command line arguments */
  145.         strcat(sys, " ");
  146.         strcat(sys, arg[n]);
  147.     }
  148.     system(buf);        /* try to invoke it */
  149. #endif
  150.     chdir(dir);
  151.     free(dir);        /* return to whence we started */
  152.     if (unlink(buf) && warn) {
  153.         printf("Cannot unsave temporary file %s\n", buf);
  154.         nerrs++;
  155.     }
  156. }
  157.